<?php
/**
 * ============================================================================
 * @copyright China International Consulting. 中国国际咨询
 * @version:  v1.0
 * @website   http://www.chinacons.com
 * ---------------------------------------------
 * $Date: 2007.12.17
*/

  if( ! defined("IN_WEB"))
  {
	 header("location: http://$_SERVER[SERVER_NAME]/siteadmin/index.php");
  }

  if ( $_POST)
  {
	$cates = implode(",", (array)$_POST['cates']);
	//查询是否有这个分类ID
	$query = "SELECT cate_id,zh_cate_title FROM $TBL_CATE WHERE cate_id IN($cates)";
	$result = mysql_query($query) or die(mysql_error());
	if($rows = mysql_num_rows($result)>0)
	{
		while($array = mysql_fetch_array($result))
		{
			//查询这些分类的所有子类，如果有子类，则将他们的父类ID 改成新ID 
			$query1 = "SELECT cate_id,parent_id FROM $TBL_CATE WHERE parent_id= '$array[cate_id]'";
			$result1 = mysql_query($query1);
			if($rows1 = mysql_num_rows($result1)>0)
			{
				while($array1 = mysql_fetch_array($result1))
				{
					//将他的子分类的父ID 全部换成新ID
					$query2 = "UPDATE $TBL_CATE SET parent_id='$cID' WHERE cate_id = '$array1[cate_id]'";
					$result2=mysql_query( $query2) or die(mysql_error());
				}
			}
			$query3 = "UPDATE $TBL_NEWS SET cate_id = '$cID' WHERE cate_id = '$array[cate_id]'";
			$result3 = mysql_query($query3);
			if($result3)
			{
				//删除这些分类
				$query = "DELETE FROM $TBL_CATE WHERE cate_id IN ( $cates)";
				$result = mysql_query( $query);
				if($result)
				{
					header("location: " . $param);
					exit(0);
				}
			}
		}
	}
	else
	{
		MyNotice("請選擇你要合并的分类");
		exit(0);
	}
  }

  $tpl->set_file("right", "unite_cate.htm");
  $tpl->set_block("right", "UNITE_LIST", "unite_list");
  
  $param = "?cID=$cID&cPath=$cPath";

  //查询当前子类下的
  mysql_data_seek($result, 0);
  while( $array = mysql_fetch_array( $result))
  {
	  if( $array['cate_id'] == $cID)
	  {
		  continue;
	  }
	  $tpl->set_var( $array);
	  $tpl->parse("unite_list", "UNITE_LIST", true);
  }

  $tpl->set_var(array( 
					   "CATE_ID" => $cate_id,
					   "CATE_CNAME" => stripslashes( $this_cate["zh_cate_title"]),
					   "EDIT_CATE" => $edit_cate,
					   "DEL_CATE" => $del_cate,
					   "MOVE_CATE" => $move_cate,
					   "HEBING_CATE" => $hebing_cate,
					   "SUB_NUM" => $sub_num,
					   "CATE_DESC" => $this_cate['cate_desc'],
					   "SON_URL" => "?cPath=$cID",
	                  ));

  $tpl->parse("Output", "right");
  $right_page = $tpl->get("Output");  
?>